using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion

	public class ChiTietKHDTRepository
	{
		public static List<ChiTietKHDT> SelectAll()
		{
			try
			{
				return DataContext.Instance.ChiTietKHDT.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<ChiTietKHDT>();
		}

		public static ChiTietKHDT SelectByID(int mactkhdt)
		{
			try
			{
				return DataContext.Instance.ChiTietKHDT.FirstOrDefault(item => item.MaCTKHDT == mactkhdt );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(ChiTietKHDT obj)
		{
			try
			{
				DataContext.Instance.ChiTietKHDT.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int mactkhdt)
		{
			try
			{
				var delitem = DataContext.Instance.ChiTietKHDT.FirstOrDefault(item => item.MaCTKHDT == mactkhdt );
				DataContext.Instance.ChiTietKHDT.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<ChiTietKHDT> RetrieveByID(int mactkhdt)
		{
			try
			{
				return (from item in DataContext.Instance.ChiTietKHDT where  item.MaCTKHDT == mactkhdt  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<ChiTietKHDT>();
		}

		public static List<ChiTietKHDT> SelectByMaKHDT(int makhdt)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.ChiTietKHDT where item.MaKHDT == makhdt select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<ChiTietKHDT>();
		}

		public static List<ChiTietKHDT> SelectByMaHP(int mahp)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.ChiTietKHDT where item.MaHP == mahp select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<ChiTietKHDT>();
		}

	}

}
